package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Product;
import com.sjlh.centurylianhua.entity.ProductType;
import com.sjlh.centurylianhua.mapper.ProducttypeMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/sptype")
public class ProducttypeController {

    @Resource
    private ProducttypeMapper typeMapper;

    /**
     * 多条件分页查询商品类别
     * @param sc
     * @return
     */
    @RequestMapping("/showType")
    public Object showByCon(@RequestBody SearchCon sc) {
        // 封装条件查询构造器
        QueryWrapper<ProductType> wrapper = new QueryWrapper<ProductType>();
        wrapper.like(StringUtils.isnotNull(sc.getName()), "name", sc.getName());
        if (sc.getPage() == null) {
            List<ProductType> list = typeMapper.selectList(wrapper);
            long count = typeMapper.selectList(wrapper).size();
            ResultData rs = new ResultData(true, 20000, "成功", list, count);
            return rs;
        } else {
            // 封装分页对象
            Page<ProductType> page = new Page<ProductType>(sc.getPage(), sc.getLimit());
            Page<ProductType> productType = typeMapper.selectPage(page, wrapper);

            List<ProductType> list = productType.getRecords();//所有数据
            long count = productType.getTotal();//总条数
            ResultData rs = new ResultData(true, 20000, "成功", list, count);
            return rs;
        }
    }

    /**
     *根据商品类别名字精确查询
     * */
    public ProductType getOne(@RequestBody ProductType producttype){
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq( "name", producttype.getName());
        return typeMapper.selectOne(wrapper);
    }

    /**
     * 添加商品类别
     * @param producttype
     * @return
     */
    @RequestMapping("/add")
    public Object addProduct(@RequestBody ProductType producttype){
        ProductType pt=this.getOne(producttype);
        if(pt == null){
            int r= typeMapper.insert(producttype);
            if(r==1)
                return new ResultData(true,20000,"添加成功",true,null);
            else
                return new ResultData(false,20000,"添加失败",false,null);
        } else {
            return new ResultData(false,20001,"该类别已存在",false,null);
        }

    }

    /**
     * 更新商品类别
     * @param producttype
     * @return
     */
    @RequestMapping("/update")
    public Object updateProductType(@RequestBody ProductType producttype){
        ProductType pt=this.getOne(producttype);
        if(pt == null){
            int r= typeMapper.updateById(producttype);
            if(r==1)
                return new ResultData(true,20000,"修改成功",true,null);
            else
                return new ResultData(false,20000,"修改失败",false,null);
        } else {
            return new ResultData(false,20001,"该类别已存在",false,null);
        }
    }

    /**
     * 删除商品类别
     * @param producttype
     * @return
     */
    @RequestMapping("/del")
    public Object delProductType(@RequestBody ProductType producttype){
        int r= typeMapper.deleteById(producttype.getId());
        if(r==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);

    }

    /**
     * 批量删除商品类别
     * @param ids
     * @return
     */
    @RequestMapping("/batchDel")
    public Object batchDelProductType(List<Integer> ids){
        int r= typeMapper.deleteBatchIds(ids);
        if(r==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);

    }

}
